package com.msos.dao.SMSQueryDao;

import com.msos.model.Message.Message;
import com.msos.model.customer.Customer;
import com.msos.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: SMSQueryDao
 * @Description: TODO
 * @Author: hsx
 * @Date: 2022/4/13 9:44
 * @Version: v1.0
 */
public class SMSQueryDao {
    QueryRunner queryRunner = new QueryRunner(DBUtils.getDs());

    //根据发送手机号码查询发送信息
    public List<Message> getAllMessage(String mid,Customer customer, int start, int size, String sort, String order) throws SQLException {
        StringBuffer sql = new StringBuffer("select * from message where ");
        Object[] params = null;
        if (mid==null){
            sql.append("(sephone=? or acphone=?) order by ? ? limit ?,?;");
            params = new Object[6];
            params[0] = customer.getPhone();
            params[1] = customer.getPhone();
            params[2] = sort;
            params[3] = order;
            params[4] = start;
            params[5] = size;
        }else {
            sql.append("(sephone=? or acphone=?) and mid=? order by ? ? limit ?,?;");
            params = new Object[7];
            params[0] = customer.getPhone();
            params[1] = customer.getPhone();
            params[2] = Integer.parseInt(mid);
            params[3] = sort;
            params[4] = order;
            params[5] = start;
            params[6] = size;
        }
        return queryRunner.query(sql.toString(),new BeanListHandler<>(Message.class),params);
    }

    public Integer deleteMessageByMid(int mid) throws SQLException {
        return queryRunner.update("delete from message where mid=?",mid);
    }

    public Long getTotal(String mid) throws SQLException {
        if (mid == null){
            return queryRunner.query("select count(*) from message",new ScalarHandler<>());
        }else {
            return queryRunner.query("select count(*) from message where mid=?",new ScalarHandler<>(),Integer.parseInt(mid));
        }
    }

    public List<Message> getSendDateBySephone(Customer customer) throws SQLException {
        return queryRunner.query("select mid,sendDate from message where sephone=? or acphone=?", new ResultSetHandler<List<Message>>() {
            @Override
            public List<Message> handle(ResultSet rs) throws SQLException {
                List<Message> list = new ArrayList();
                Message message = null;
                while (rs.next()){
                    message = new Message();
                    message.setMid(rs.getInt("mid"));
                    message.setSendDate(rs.getString("sendDate"));
                    list.add(message);
                }
                return list;
            }
        },customer.getPhone(),customer.getPhone());
    }
}
